import React, { Component } from 'react';
import { Row, Col, Popover } from 'antd';
import { observer, inject } from 'mobx-react';
import { toJS } from 'mobx';
import { WeaTree } from 'ecCom';
import '../../../workflow/util/windowFun';

@inject('centerStore')
@observer
class OdocCenterLibraryComp extends Component {
  componentDidMount () {
    const { centerStore } = this.props;
    centerStore.buildCreateReq();
    centerStore.buildCreateOffical();
  }

  render () {
    return (
      this.renderCreateReq()
    );
  }

  // 渲染 公文创建 大类
  renderCreateReq () {
    const { centerStore } = this.props;
    let createReq = toJS(centerStore.createReq);
    let createOffical = toJS(centerStore.createOffical);

    let isShowCreateReqDraftDialog = createOffical[0].createOfficalList.length > 0;
    let isShowCreateReqReceiptDialog = createOffical[1].createOfficalList.length > 0;

    const content1 = (
      <div>
        {this.renderCreateReqTree(createOffical[0].createOfficalList, createOffical[0].createOfficalListKeys, 'popover1')}
      </div>
    );
    const content2 = (
      <div>
        {this.renderCreateReqTree(createOffical[1].createOfficalList, createOffical[1].createOfficalListKeys, 'popover2')}
      </div>
    );


    return (<Row ecId={`${this && this.props && this.props.ecId || ''}_Row@9123my`} className="wea-odoc-center-left-row">
      <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@gybwma`} span="12" >
        {
          isShowCreateReqDraftDialog &&
          <Popover ecId={`${this && this.props && this.props.ecId || ''}_Popover@3bba12`} overlayClassName="wea-odoc-center-left-popover" placement="bottomLeft" content={content1} trigger="click" visible={centerStore.popover1} onVisibleChange={bool => centerStore.popoverChange(bool, 'popover1')}>
            <div className="wea-odoc-center-bigbutton-wrapper"> <div className="wea-odoc-center-bigbutton1"><span>{createReq.createReqDraft.showName}</span></div></div>
          </Popover>
        }
        {
          !isShowCreateReqDraftDialog &&
          <div className="wea-odoc-center-bigbutton-wrapper"> <div className="wea-odoc-center-bigbutton1"><span>{createReq.createReqDraft.showName}</span></div></div>
        }
      </Col>
      <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@trbt5x`} span="12">
        {
          isShowCreateReqReceiptDialog &&
          <Popover ecId={`${this && this.props && this.props.ecId || ''}_Popover@hly4ff`} overlayClassName="wea-odoc-center-left-popover" placement="bottomLeft" content={content2} trigger="click" visible={centerStore.popover2} onVisibleChange={bool => centerStore.popoverChange(bool, 'popover2')}>
            <div className="wea-odoc-center-bigbutton-wrapper"><div className="wea-odoc-center-bigbutton2" ><span>{createReq.createReqReceipt.showName}</span></div></div>
          </Popover>
        }
        {
          !isShowCreateReqReceiptDialog &&
          <div className="wea-odoc-center-bigbutton-wrapper"><div className="wea-odoc-center-bigbutton2" ><span>{createReq.createReqReceipt.showName}</span></div></div>
        }

      </Col>
    </Row>);
  }

  renderCreateReqTree (treeDatas, expandedKeys, key) {
    const { centerStore } = this.props;
    return (
      <WeaTree ecId={`${this && this.props && this.props.ecId || ''}_WeaTree@yizo9b`}
        datas={treeDatas}
        expandedKeys={expandedKeys}
        hasRadio={true}
        renderNode={this.renderNode}
        onSelect={(ids, nodeids, nodes) => this.onSelect(ids, nodeids, nodes, key)}
      />);
  }

  renderNode = (node) => {
    if (node.haschild) {
      return <span className="wea-odoc-center-left-tree-parentnode" title={node.name}>{node.name}</span>;
    }
    return <span title={node.name}>{node.name}</span>;
  }

  onSelect = (ids, nodeids, nodes, key) => {
    const { centerStore } = this.props;
    let node = nodes[0];
    if (!node.haschild) {
      centerStore.popoverChange(false, key);
      centerStore.addWorkflowUseCount(node.workflowid);
      // const hasWorkflowId = toJS(centerStore.unSupportSPAWf).includes(node.workflowid);
      // const url = hasWorkflowId ? "/spa/workflow/static4form/index.html?_rdm="+new Date().getTime()+"#/main/workflow/req?iscreate=1&" : "/workflow/request/CreateRequestForward.jsp?";
      let paramstr = `workflowid=${node.workflowid}&isagent=${node.isagent}&beagenter=${node.beagenters}&f_weaver_belongto_userid=${node.belongtoUsers}&f_weaver_belongto_usertype=0`;
      // openFullWindowHaveBar(url+paramstr);
      openFullWindowHaveBar(`/workflow/request/CreateRequestForward.jsp?${paramstr}`);
    }
  }
}
export default OdocCenterLibraryComp;
